package com.hht.tools.uilib

import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.View.OnClickListener
import android.view.View.OnTouchListener
import androidx.annotation.RequiresApi
import androidx.core.view.ViewCompat
import com.hht.toolanduilibs.showLog
import com.hht.toolanduilibs.showToast
import com.hht.toolanduilibs.widget.StateBarView
import com.hht.tools.R
import com.hht.tools.databinding.HomeStateBarViewBinding


/**
 * Author:chen'hu
 * Time:2023/8/7 13:38
 * Description:This is HomeStateBarView
 */
@RequiresApi(Build.VERSION_CODES.N)
class HomeStateBarView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null,
) : StateBarView(context, attrs), OnClickListener, OnTouchListener {
    private lateinit var binding: HomeStateBarViewBinding
    private val mWifiWeakLevel = intArrayOf(
        R.drawable.weak_one_wifi_icon_selector,
        R.drawable.weak_two_wifi_icon_selector,
        R.drawable.weak_three_wifi_icon_selector,
        R.drawable.weak_four_wifi_icon_selector
    )

    init {
        initView()
    }

    @RequiresApi(Build.VERSION_CODES.N)
    fun initView() {
        val inflater = LayoutInflater.from(context)
        inflater.inflate(R.layout.home_state_bar_view, this)
        binding = HomeStateBarViewBinding.inflate(inflater, this, true)
        binding.homeBarIvWifi.setOnClickListener(this)
        binding.homeBarIvWifi.setOnTouchListener(this)
        binding.homeBarIvUsb.setOnClickListener(this)
        binding.homeBarIvUsb.setOnTouchListener(this)
        binding.homeBarIvNet.setOnClickListener(this)
        binding.homeBarIvNet.setOnTouchListener(this)
        binding.homeBarIvHotSpot.setOnClickListener(this)
        binding.homeBarIvHotSpot.setOnTouchListener(this)
        binding.homeBarIvPowerOff.setOnClickListener(this)
        binding.homeBarIvPowerOff.setOnTouchListener(this)
        binding.homeBarIvBluetoothOpen.setOnClickListener(this)
        binding.homeBarIvBluetoothOpen.setOnTouchListener(this)
        updateStateView()
        setImageIcon()
    }

    private fun setImageIcon() {
        binding.homeBarIvPowerOff.setImageResource(R.drawable.poweroff_icon_selector)
        binding.homeBarIvWifi.setImageResource(R.drawable.weak_four_wifi_icon_selector)
        binding.homeBarIvBluetoothOpen.setImageResource(R.drawable.bluetooth_icon_selector)
        binding.homeBarIvNet.setImageResource(R.drawable.net_icon_selector)
        binding.homeBarIvHotSpot.setImageResource(R.drawable.hot_spot_icon_selector)
        binding.homeBarIvUsb.setImageResource(R.drawable.usb_icon_selector)
    }


    override fun updatePowerOffState(showView: Boolean) {
        binding.homeBarIvPowerOff?.visibility = if (showView) VISIBLE else GONE
    }


    override fun updateInterNetState(internetState: Boolean) {
        binding.homeBarIvNet?.visibility = if (internetState) VISIBLE else GONE
    }

    override fun updateWifiState(wifiState: Boolean, level: Int) {
        binding.homeBarIvWifi?.apply {
            visibility = if (wifiState) VISIBLE else GONE
            if (wifiState) {
                if (level in 0..3) {
                    setImageResource(mWifiWeakLevel[level])
                }
            } else {
                setImageResource(R.drawable.no_wifi_icon_selector)
            }
        }


    }

    override fun updateBluetoothState(btState: Boolean) {
//        "蓝牙 $btState".showLog()
        binding.homeBarIvBluetoothOpen?.visibility = if (btState) VISIBLE else GONE
    }

    override fun updateHotSpotState(hotState: Boolean) {
//        "热点 $hotState".showLog()
        binding.homeBarIvHotSpot?.visibility = if (hotState) VISIBLE else GONE
    }

    override fun updateUsbState(usbState: Boolean) {
        binding.homeBarIvUsb?.visibility = if (usbState) VISIBLE else GONE
    }

    override fun updateNotificationState(notifyState: Boolean) {
//        "notifyState $notifyState".showLog()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.home_bar_iv_power_off -> "pwwerOff".showToast(context)
            R.id.home_bar_iv_net -> "Net".showToast(context)
            R.id.home_bar_iv_wifi -> "Wifi".showToast(context)
            R.id.home_bar_iv_bluetooth_open -> "Bluetooth".showToast(context)
            R.id.home_bar_iv_usb -> "usb".showToast(context)
            R.id.home_bar_iv_hot_spot -> "HotSpot".showToast(context)
        }
    }

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        when (event?.action) {
            KeyEvent.ACTION_DOWN -> ViewCompat.animate(v!!).setDuration(30).scaleX(0.8f)
                .scaleY(0.8f).start()

            KeyEvent.ACTION_UP -> ViewCompat.animate(v!!).setDuration(30).scaleX(1.0f).scaleY(1.0f)
                .start()
        }
        return false
    }
}